package org.xgj.zhangben;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.xgj.zhangben.util.Constant;

public class BaseService {
	protected static final String SUCCESS = "success";
	protected static final String INPUT = "input";
	protected static final String INIT = "init";
	protected static final String JSON = "json";
	protected static final String PAGE = "page";
	protected static final String FILEUPLOAD = "fileUpload";
	protected static final String APPLETVIEW = "appletview";
	protected static final String APPLET = "applet";
	protected static final String OUTEXCEL = "outexcel";
	protected static final String DOWNLOAD = "download";
	protected static final String JUMP = "jump";

	private List<String> errors = new ArrayList<String>();

	/**
	 * 获取request
	 */
	protected HttpServletRequest getRequset() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获取验证码
	 * 
	 * @return
	 */
	protected String getVerifyCode() {
		return (String) getAttrBySession(Constant.VERIFY_CODE);
	}

	/**
	 * 把数据存放在request里面
	 * 
	 * @param name
	 * @param value
	 */
	protected void setAttrByRequest(String key, Object value) {
		ServletActionContext.getRequest().setAttribute(key, value);
	}

	/**
	 * 把数据存放在session里面
	 * 
	 * @param name
	 * @param value
	 */
	protected void setAttrBySession(String key, Object value) {
		ServletActionContext.getRequest().getSession().setAttribute(key, value);
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected Object getAttrByRequest(String name) {
		return ServletActionContext.getRequest().getAttribute(name);
	}

	/**
	 * 从session取出数据
	 * 
	 * @param name
	 * @param value
	 */
	protected Object getAttrBySession(String name) {
		return ServletActionContext.getRequest().getSession()
				.getAttribute(name);
	}

	protected Boolean nullID(BaseEntity base) {
		return base.getId() == null;
	}

	/**
	 * 存放用户
	 * 
	 * @param name
	 * @param value
	 */
	protected void putLoginedUser(LoginedUser loginedUser) {
		ServletActionContext.getRequest().getSession().setAttribute(
				Constant.LOGINED_USER, loginedUser);
	}

	/**
	 * 返回登录的用户
	 * 
	 * @param name
	 * @param value
	 */
	protected LoginedUser getLoginedUser() {
		LoginedUser loginedUser = (LoginedUser) getAttrBySession(Constant.LOGINED_USER);
		if (loginedUser == null) {
			loginedUser = new LoginedUser();
			putLoginedUser(loginedUser);
		}
		return loginedUser;
	}

	/**
	 * 插入错误信息到errors
	 * 
	 * @param errorInfo
	 */
	protected void addErrorInfo(String errorInfo) {
		errors.add(errorInfo);
	}

	/**
	 * 得到errors的集合
	 * 
	 */
	protected List<String> getErros() {
		return errors;
	}

	/**
	 * 集合是否为空
	 */
	protected boolean havaError() {
		return !errors.isEmpty();
	}

	/**
	 * 
	 */
	protected void errorToMsgInRequest() {
		ServletActionContext.getRequest().setAttribute("msg", errorsToStr());
	}

	/**
	 * 返回错误结果集
	 * 
	 * @return
	 */
	public String errorsToStr() {
		if (errors.isEmpty()) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < errors.size(); i++) {
			sb.append(String.format("%1s：%2s\\n", i + 1, errors.get(i)));
		}
		return sb.toString();
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected String getParam(String name) {
		return ServletActionContext.getRequest().getParameter(name);
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected String getParam(String name, String defaultValue) {
		String strTemp = ServletActionContext.getRequest().getParameter(name);
		if (strTemp == null || strTemp.equals("")) {
			strTemp = defaultValue;
		}
		return strTemp;
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected Integer getIntParam(String name) {
		return Integer.parseInt(ServletActionContext.getRequest().getParameter(
				name));
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected Integer getIntParam(String name, Integer defaultValue) {
		String strTemp = ServletActionContext.getRequest().getParameter(name);
		if (strTemp != null) {
			return Integer.parseInt(ServletActionContext.getRequest()
					.getParameter(name));
		} else {
			return defaultValue;
		}
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected boolean getBoolParam(String name) {
		return Boolean.parseBoolean(ServletActionContext.getRequest()
				.getParameter(name));
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected boolean getBoolParam(String name, Boolean defaultValue) {
		String strTemp = ServletActionContext.getRequest().getParameter(name);
		if (strTemp != null) {
			return Boolean.parseBoolean(ServletActionContext.getRequest()
					.getParameter(name));
		} else {
			return defaultValue;
		}
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected float getFloatParam(String name) {
		return Float.parseFloat(ServletActionContext.getRequest().getParameter(
				name));
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected float getFloatParam(String name, Float defaultValue) {
		String strTemp = ServletActionContext.getRequest().getParameter(name);
		if (strTemp != null) {
			return Float.parseFloat(ServletActionContext.getRequest()
					.getParameter(name));
		} else {
			return defaultValue;
		}
	}

	/**
	 * 
	 */
	protected double getDoubleParam(String name) {
		return Double.parseDouble(ServletActionContext.getRequest()
				.getParameter(name));
	}

	/**
	 * 
	 */
	protected double getDoubleParam(String name, Double defaultValue) {
		String strTemp = ServletActionContext.getRequest().getParameter(name);
		if (strTemp != null) {
			return Double.parseDouble(ServletActionContext.getRequest()
					.getParameter(name));
		} else {
			return defaultValue;
		}
	}

	/**
	 * 
	 */
	protected long getLongParam(String name) {
		return Long.parseLong(ServletActionContext.getRequest().getParameter(
				name));
	}

	/**
	 * 
	 */
	protected long getLongParam(String name, Long defaultValue) {
		String strTemp = ServletActionContext.getRequest().getParameter(name);
		if (strTemp != null) {
			return Long.parseLong(ServletActionContext.getRequest()
					.getParameter(name));
		} else {
			return defaultValue;
		}
	}

	/**
	 * 返回集合 比如前台传的 name1,name2,name3的形式，
	 * 
	 * @param name
	 * @param separatorRegex
	 *            分隔符
	 * @return
	 */
	protected List<String> getListParam(String name, String separatorRegex) {
		String strTemp = getParam(name);
		List<String> list = new ArrayList<String>();
		if (strTemp != null) {
			list.addAll(Arrays.asList(strTemp.split(separatorRegex)));
		}
		return list;
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected String[] getParams(String name) {
		return ServletActionContext.getRequest().getParameterValues(name);
	}

}
